package com.jietong.rfid.uhf.mode;

import java.net.Socket;
import java.nio.ByteBuffer;

import android_serialport_api.SerialPortDevice;
import android_serialport_api.service.SerialPortService;
import android_serialport_api.service.impl.SerialPortServiceImpl;

import com.jietong.rfid.util.DataConvert;

public class BasePackage {
	
	/**
	 * 4 Channel Antenna
	 * 8 Channel Antenna
	 * 16 Channel Antenna
	 * 32 Channel Antenna
	 */
	public int channel = -1;
	
	protected SerialPortDevice serialPort;
	protected Socket socket;
	
	byte[] comReceiveData(SerialPortDevice serialPort) {
		SerialPortService serialPortService = new SerialPortServiceImpl();
		byte[] result = serialPortService.read(serialPort);
		//pintReceiveData(result,1);
		return result;
	}
	
	public void pintReceiveData(byte[] data,int stauts) {
		String type = "Send cmd : ";
		if(stauts == 1){
			type = "Receive cmd : ";
		}
		if(null != data){
			System.out.println(type + DataConvert.cardNoAddSpace(DataConvert.bytesToHexString(data)));
		}
	}
	
	public void printData(ParseData parse,byte[] readData) {
		StringBuffer sb = new StringBuffer();
		sb.append("CMD " + DataConvert.bytesToHexString(parse.cmd));
		sb.append(" Data length " + readData.length);
		sb.append(" Data " + DataConvert.bytesToHexString(readData));
		System.out.println(sb.toString());
	}
	
	public  boolean comSendData(SerialPortDevice serialPort, byte[] sendData) {
		//pintReceiveData(sendData,0) ;
		SerialPortService serialPortService = new SerialPortServiceImpl();
		return serialPortService.send(serialPort, sendData);
	}
	
	class ReceiveData {
		
		private byte cmd;
		private byte[] data;
		private int length;
		private String communicationMode;

		public ReceiveData(byte cmd, int length, byte[] data,String communicationMode) {
			this.cmd = cmd;
			this.length = length;
			this.data = data;
			this.communicationMode = communicationMode;
		}

		public ReceiveData() {

		}

		public byte getCmd() {
			return cmd;
		}

		public void setCmd(byte cmd) {
			this.cmd = cmd;
		}

		public byte[] getData() {
			return data;
		}

		public void setData(byte[] data) {
			this.data = data;
		}

		public int getLength() {
			return length;
		}

		public void setLength(int length) {
			this.length = length;
		}
		
		public String getCommunicationMode() {
			return communicationMode;
		}

		public void setCommunicationMode(String communicationMode) {
			this.communicationMode = communicationMode;
		}
	}
	
	public class ParseData {
		public byte startcode;
		public byte deviceNo;
		public byte cmd;
		public byte bcc;
		public byte seq;
		public int len;
		public int headCount = 0;
		public int dataCount = 0;
		public ByteBuffer buffer = ByteBuffer.allocate(500);
		public ByteBuffer bufferLen = ByteBuffer.allocate(1);
	}
}
